package com.ewerp.mud.misc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import com.ewerp.mud.BaseMockObject;
import com.ewerp.mud.IBaseMockObject;

public class MockServerSocket extends ServerSocket implements IBaseMockObject {
    protected BaseMockObject bmo = new BaseMockObject();
    protected int sessionsToCreate = 0;
    protected int sessionsCreated = 0;
    
    public MockServerSocket() throws IOException {
        super();
    }
    
    @Override
    public void setSoTimeout(int val) {
        bmo.addMethodInvocation("setSoTimeout", new Object[] { val });
    }
    
    @Override
    public Socket accept() throws IOException {
        if(sessionsCreated < sessionsToCreate) {
            sessionsCreated++;
            return new MockSocket();
        } else {
            // Block
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new IOException(e);
            }
            return null;
        }
    }
    
    public void setSessionsToCreate(int sessionsToCreate) {
        this.sessionsToCreate = sessionsToCreate;
    }

    @Override
    public int getMethodInvocationCount(String methodName) {
        return bmo.getMethodInvocationCount(methodName);
    }

    @Override
    public List<Object[]> getMethodInvocationParameters(String methodName) {
        return bmo.getMethodInvocationParameters(methodName);
    }

}
